<template>
  <section>
    <div class="content-top">
      <div class="content-title">{{ title }}</div>
      <div @click="handleList" class="memo-fn">
        显示模式
      </div>
    </div>
    <div class="memo" v-if="listType == 0">
      <div class="memo-title" v-if="finished">
        {{ memoDetail.title }}
      </div>
      <div class="memo-wrap" v-if="showContent" v-html="showContent"></div>
      <div class="memo-contronls">
        <div @click="handleAdd">
          <el-icon>
            <component :is="elicons['Plus']" />
          </el-icon>
        </div>
        <div @click="lastMemo">
          <el-icon>
            <component :is="elicons['ArrowLeftBold']" />
          </el-icon>
        </div>
        <div @click="nextMemo">
          <el-icon>
            <component :is="elicons['ArrowRightBold']" />
          </el-icon>
        </div>
        <div @click="handleEdit">
          <el-icon>
            <component :is="elicons['Edit']" />
          </el-icon>
        </div>
        <!-- <div @click="handleList">
          <el-icon>
            <component :is="elicons['ScaleToOriginal']" />
          </el-icon>
        </div> -->
      </div>
    </div>
    <div :class="listClass" v-else>
      <div v-for="x in list">
        {{ x.title }}
      </div>
    </div>
    <el-dialog title="数据详情" v-model="isEdit" width="800px" draggable>
      <div class="content-form">
        <div>备忘录标题</div>
        <div>
          <el-input v-model="inputTitle" style="width: 200px;"></el-input>
        </div>
      </div>
      <editor :valueHtml="inputContent" @change="changeHtml" />
      <div slot="footer" class="dialog-footer" style="display: flex;justify-content: right;margin-top: 10px;">
        <el-button type="primary" @click="save">保 存</el-button>
        <el-button @click="() => { isEdit = false }">取 消</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script setup>
import { computed, getCurrentInstance, onMounted, ref } from 'vue';
import editor from '../common/editor.vue';
import elicons from '@/utils/elicons';
const props = defineProps({
  title: {
    type: String,
    defalut: ''
  }
})
const { proxy } = getCurrentInstance()
const showContent = ref('')
const inputTitle = ref('')
const inputContent = ref('')
const list = ref([])
const isEdit = ref(false)
const finished = ref(false)
const memoDetail = ref(null)
const listIdx = ref(0)
const formType = ref(0)
const listType = ref(0)
const path = ref('D:/cyc/files/memo')

onMounted(() => {
  getMemoList()
})

function getMemoList() {
  proxy.$service.getAllData('memos').then(res => {
    list.value = res.data
    memoDetail.value = res.data[0]
    finished.value = true
    getShowContent()
  })
}

const listClass = computed(() => {
  let str = ''
  switch (listType.value) {
    case 1:
      str = 'line-list'
      break;
    case 2:
      str = 'block-list'
      break;
  }
  return str
})

function handleAdd() {
  formType.value = 1
  inputContent.value = ' '
  inputTitle.value = ''
  isEdit.value = true
}

function handleEdit() {
  formType.value = 2
  inputTitle.value = memoDetail.value.title
  getShowContent()
  inputContent.value = showContent.value
  isEdit.value = true
}

function handleList() {
  if (listType.value > 1) {
    listType.value = 0
  } else {
    listType.value += 1
  }
}

function save() {
  const data = {
    title: inputTitle.value,
    update_time: proxy.$global.formatDataTime(new Date())
  }
  if (formType.value == 2) {
    data.id = memoDetail.value.id
    proxy.$service.editData({ table: 'memos', data }).then(res => {
      rewrite(memoDetail.value.name)
    })
  } else {
    const filename = proxy.$global.fileName('memo')
    data.create_time = proxy.$global.formatDataTime(new Date())
    data.name = filename
    data.sort = list.value.length + 1
    data.datestr = proxy.$global.getDate()
    proxy.$service.addData({ table: 'memos', data }).then(res => {
      rewrite(filename)
      listIdx.value = data.sort
    })
  }
}

function rewrite(filename) {
  let filePath = path.value + '/' + filename + '.txt'
  proxy.$system.writeFile(filePath, inputContent.value).then(res2 => {
    proxy.$global.success('编辑成功！')
    isEdit.value = false
    getMemoList()
  })
}

function getShowContent() {
  memoDetail.value = list.value[listIdx.value]
  let filePath = path.value + '/' + memoDetail.value.name + '.txt'
  proxy.$system.readFile(filePath).then(res => {
    showContent.value = res
  })
}

function nextMemo() {
  if (listIdx.value < list.value.length - 1) {
    listIdx.value += 1
    getShowContent()
  }
}

function lastMemo() {
  if (listIdx.value > 0) {
    listIdx.value -= 1
    getShowContent()
  }
}

function changeHtml(e) {
  inputContent.value = e
}

</script>
<style scoped lang="scss">
.content-form {
  display: flex;
  margin-bottom: 10px;

  >div {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.memo-fn {
  cursor: pointer;
  user-select: none;
}

.memo {
  width: 100%;
  min-height: 300px;
  background: linear-gradient(to top, #fbfbfb, #fff);
  padding: 10px;
  border-radius: 10px;
  position: relative;

  .memo-title {
    width: 100%;
    border-bottom: 1px solid #ccc;
    padding: 10px 0;
    color: #000;
    margin-bottom: 5px;
  }

  .memo-wrap {
    word-break: break-word;
    color: #000;
  }

  .memo-contronls {
    display: flex;
    width: 80%;
    justify-content: space-around;
    margin: 0 auto;
    position: absolute;
    bottom: 10px;
    left: 10%;

    >div {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      border: 1px solid #666;
      color: #666;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
    }
  }
}

.line-list {
  >div {
    height: 40px;
    line-height: 40px;
  }
}

.block-list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;

  >div {
    width: 30%;
    border: 1px solid #fff;
    margin-bottom: 10px;
    text-align: center;
    padding: 10px 0;
  }
}
</style>
